#ifndef STX_PLATFORM_POSIX_THREAD_CONDITIONAL_VARIABLE_ATTRIBUTES_HPP
#define STX_PLATFORM_POSIX_THREAD_CONDITIONAL_VARIABLE_ATTRIBUTES_HPP

#include <stx/platform/posix/thread/common.hpp>

namespace stx {

namespace posix {

class conditional_variable_attributes: noncopyable
{
public:
    
    typedef conditional_variable_attributes this_type;
    
    conditional_variable_attributes()
    {
        int ret = pthread_condattr_init(&handle_);
        if (ret) {
            STX_THROW_SYSTEM_ERROR_VAL(ret, "pthread_condattr_init");
        }
    }
    
    ~conditional_variable_attributes()
    {
        int ret = pthread_condattr_destroy(&handle_);
        if (ret) {
            STX_DESTRUCTOR_SYSTEM_ERROR_VAL(ret, "pthread_condattr_destroy");
        }
    }
    
    #if STX_HAS_PTHREAD_CONDATTR_GETCLOCK
    clockid_t clock() const
    {
        clockid_t val;
        int ret = pthread_condattr_getclock(&handle_, &val);
        if (ret) {
            STX_THROW_SYSTEM_ERROR_VAL(ret, "pthread_condattr_getclock");
        }
        return val;
    }
    #endif
    
    #if STX_HAS_PTHREAD_CONDATTR_SETCLOCK
    this_type& set_clock(clockid_t val)
    {
        int ret = pthread_condattr_setclock(&handle_, val);
        if (ret) {
            STX_THROW_SYSTEM_ERROR_VAL(ret, "pthread_condattr_setclock");
        }
        return *this;
    }
    #endif
    
    //  Values:
    //      PTHREAD_PROCESS_SHARED
    //      PTHREAD_PROCESS_PRIVATE
    int process_shared() const
    {
        int val;
        int ret = pthread_condattr_getpshared(&handle_, &val);
        if (ret) {
            STX_THROW_SYSTEM_ERROR_VAL(ret, "pthread_condattr_getpshared");
        }
        return val;
    }
    
    this_type& set_process_shared(int val)
    {
        int ret = pthread_condattr_setpshared(&handle_, val);
        if (ret) {
            STX_THROW_SYSTEM_ERROR_VAL(ret, "pthread_condattr_setpshared");
        }
        return *this;
    }
    
    typedef pthread_condattr_t handle_type;
    
    handle_type* handle()
    {
        return &handle_;
    }
    
    const handle_type* handle() const
    {
        return &handle_;
    }
    
private:
    
    handle_type handle_;
};

} // namespace posix

} // namespace stx

#endif // STX_PLATFORM_POSIX_THREAD_CONDITIONAL_VARIABLE_ATTRIBUTES_HPP
